<table class="align-center product-info">
  <tr>
    <th width="10%"><?php echo L('goods_img')?></th>
    <th width="20%"><?php echo L('productName')?></th>
    <th width="10%"><?php echo L('goods_price')?></th>
    <th width="20%"><?php echo L('goods_number')?></th>
    <th width="20%"><?php echo L('amount_about')?></th>
    <th width="20%"><?php echo L('_ACTION_')?></th>
  </tr>
	<?php
    if (isset($cartInfo) && isset($cartInfo['productList']) && $cartInfo['productList']) :
      $i=0;
      foreach ($cartInfo['productList'] as $k => $cart) :
        $product = $cart['product'];
        $i++;
  ?>
  <tr>
    <td>
      <img src="<?php echo $product['small_img'] ?>" alt="<?php echo isset($product['name']) ? $product['name'] : null ?>" />
    </td>
    <td>
      <a href="<?php echo $product['url'];?>" style="margin-right:10px;"><?php echo isset($product['name']) ? $product['name'] : null ?></a>
    </td>
    <td><?php echo currency($product['price']); ?></td>
    <td>
      <div class="product-count-wrap">
        <span class="decrease js-decrease" data-goods-id="<?php echo $cart['id']; ?>">－</span>
        <span class="count-num"><?php echo isset($cart['qty']) ? $cart['qty'] : 1; ?></span>
        <span class="increase js-increase" data-goods-id="<?php echo $cart['id']; ?>">＋</span>
      </div>
    </td>
    <td><?php echo currency($product['price']*$cart['qty']); ?></td>
    <td>
      <?php
        if($cart['custom_type']==2):
      ?>
      <a href="javascript:;" class="js-edit-product"  data-product-id="<?php echo $product['id']; ?>" data-cart-id="<?php echo $cart['id']; ?>">编辑</a>
      |
      <?php
        endif;
      ?>
      <a href="javascript:;" class="js-delete" data-cart-id="<?php echo $cart['id'];?>">删除</a>
    </td>
  </tr>
  <?php endforeach;else:?>
  <tr>
    <td colspan="6" class="align-center"><?php echo L('nothing_in_cart_partner')?></td>
  </tr>
	<?php endif;?>
  <!--结算-->
  <tr>
    <td colspan="3" class="align-right"><?php echo L('goods_amount_total'); ?>：</td>
    <td colspan="3" class="align-left js-goods-amount"><?php echo isset($cartInfo['total_amount']) ? currency($cartInfo['total_amount']) : 0; ?></td>
  </tr>
  <tr>
    <td colspan="3" class="align-right">附加费：</td>
    <td colspan="3" class="align-left js-surcharge">￥0</td>
  </tr>
  <tr>
    <td colspan="3" class="align-right"><?php echo L('shipping_fee'); ?>：</td>
    <td colspan="3" class="align-left">
      <span class="js-freight"><?php echo isset($shipping_fee) ? currency($shipping_fee) : 0; ?></span>
      <?php
        if(isset($shippingList) && $shippingList):
          $i=0;
          foreach ($shippingList as $key=>$shipping):
           if (!$shipping['status']) continue;
            $i++;
      ?>
      <label class="ml5x">
        <input type="radio" name="shipping_method"<?php if ($key==0 || $shipping['name'] == $shipping_method_val){?> checked<?php } ?> value="<?php echo $shipping['name']; ?>"><?php echo isset($shipping['title']) ? $shipping['title'] : null; ?>
      </label>
      <?php
          endforeach;
        endif;
      ?>
    </td>
  </tr>
  <tr>
    <td colspan="3" class="align-right"><?php echo L('order_to_pay_amount'); ?>：</td>
    <td colspan="3" class="align-left js-amount-payable"><?php $total_amount = $cartInfo['total_amount']+$shipping_fee-$hongbao_money ; echo isset($total_amount) ? currency($total_amount) : 0; ?></td>
  </tr>
</table>
<input type="hidden" name="goods_amount" id="goods_amount" value="<?php echo isset($cartInfo['total_amount']) ? $cartInfo['total_amount'] : '0'; ?>" />
<input type="hidden" name="goods_number" id="goods_number" value="<?php echo isset($cartInfo['total_number']) ? $cartInfo['total_number'] : '0'; ?>" />
<input type="hidden" name="goods_weight" id="goods_weight" value="<?php echo isset($cartInfo['total_weight']) ? $cartInfo['total_weight'] : '0'; ?>" />
<input name="shipping_fee" id="input_shipping_money" type="hidden" value="<?php echo isset($shipping_fee) ? $shipping_fee : '0'; ?>" />
<input name="payment_fee" id="input_payment_money" type="hidden" value="<?php echo isset($payment_fee) ? $payment_fee : '0'; ?>" />
<input type="hidden" name="payment_method" value="Alipay">
<input type="hidden" name="no_pay" value="1">
<!--支付方式-->
<!--
<div class="item-title"><?php echo L('payment_method');?></div>
<div class="clearfix payment-method">
  <?php
    if(isset($paymentList) && $paymentList):
    $i=1;
    foreach ($paymentList as $k => $payment) :
    if (!$payment['status']) continue;
  ?>
  <div class="col-md-4">
    <label>
      <input type="radio" name="payment_method"<?php if ($i==1 || $payment['name'] == $payment_method_val){?> checked<?php } ?> value="<?php echo $payment['name'];?>">
      <?php if($payment['name'] =="Visabill"){echo L('_credit_');}elseif($payment['name'] == "Alipay"){echo L('_paypal_');}elseif($payment['name'] == "Worldpay"){echo L('_worldpay_');};?>
    </label>
  </div>
  <?php
    $i++;
    endforeach;
    endif;
  ?>
</div>
-->
<script>
function updateAmount() {//根据附加费计算应付金额
  var digitReg = /[0-9]+(\.{1}[0-9]+|\.{0}[0-9]{0})/,
      $goodsAmount = $('.js-goods-amount'),
      goodsAmount = (goodsAmount = digitReg.exec($goodsAmount.text())) && goodsAmount[0],
      $surcharge = $('.js-surcharge'),
      $freight = $('.js-freight'),
      freight = (freight = digitReg.exec($freight.text())) && freight[0],
      $amountPayable = $('.js-amount-payable'),
      amountPayable = (amountPayable = digitReg.exec($amountPayable.text())) && amountPayable[0];

  return $.ajax({
    url: '/payment/getPaymentMoney_partener',
    type: 'post',
    dataType: 'json',
    data: {amount: goodsAmount}
  })
  .done(function(data) {
    var paymentfee = data.paymentfee,
        discountRate = data.discount_rate;

    paymentc($surcharge, paymentfee);
    paymentc($amountPayable, (Number(goodsAmount) + Number(paymentfee)) * discountRate + Number(freight));
  })
  .fail(function() {
    var $networkError = $('<p class="mt1x align-center error msg">' + networkError + '</p>').insertAfter(popupBox.find('.js-popup-ft'));
    setTimeout(function () {
      $networkError.remove();
    }, 2000);
  });
}

$(function () {
  function update_cart(cid, qty) {
    return $.ajax({
      type: 'GET',
      url: '/cart/update_cart_product_ajax_partner',
      data:{'cart_id':cid,'qty':qty}
    })
    .done(function () {
      cartShow();
    });
  }

  $('input[name="total_number"]', window.parent.document).val('<?php echo $cartInfo['total_number']?>');

  updateAmount();

  $('.js-edit-product').popupBox({
    quickClose: true,
    button: ['确认', '取消'],
    content: function (trigger) {
      var pid = trigger.data('product-id');
      var cid = trigger.data('cart-id');
      var url = '<?php echo url('product/rewrite_custom');?>/partner/1/id/'+pid+'/cid/'+cid;
      return iframeHtml(url);
    },
    beforeShow: function (trigger, popupBox) {
      popupBox.addClass('edit-goods');
    },
    onOk: function(trigger, popupBox) {
      var pid = trigger.data('product-id');
      var cid = trigger.data('cart-id');
      $.ajax({
        type: 'GET',
        url: '/cart/productpost_partner',
        data: {'pid':pid, 'cid':cid},
        dataType: 'json'
      })
      .done(function(content) {
        cartShow(content);
      });

      updateAmount();
    }
  });

  $('.js-delete').popupBox({
    quickClose: true,
    button: ["<?php echo L('determine');?>", "<?php echo L('_ORDER_CANCEL_');?>"],
    content: "<?php echo L('del_goods_confirm'); ?>",
    beforeShow: function (trigger, popupBox) {
      popupBox.addClass('align-center').css('max-width', 500);
    },
    onOk: function(trigger) {
      var cartId = trigger.data('cart-id');
      $.ajax({
        type: 'GET',
        url: '/cart/deleteproductfromcart_partner',
        data:{'cart_id': cartId},
        success: function () {
          cartShow();
        }
      })
      .then(function () {
        updateAmount();
      });
    }
  });

  $('.js-decrease').on('click', function() { //数量递减
    var cid = $(this).data('goodsId');
    var _self = $(this),
    count_num = _self.siblings(".count-num");
    var qty = parseInt($.text(count_num));
    if (qty > 1) {
      update_cart(cid,(qty-1)).then(function () {
        updateAmount();
      });
    }
  });

  $('.js-increase').on('click', function() { //数量递增
    var cid = $(this).data('goodsId');
    var _self = $(this),
    count_num = _self.siblings(".count-num");
    var qty = parseInt($.text(count_num));
    update_cart(cid,(qty+1)).then(function () {
      updateAmount();
    });
  });
  $('.js-order-add-form').find( 'input[name=shipping_method]').unbind('click').click(function() {
    cartShow();
  });
  /*
  $('.js-order-add-form').on('click', 'input[name=shipping_method]', function() {
    cartShow();
  });
  */
});
</script>